import 'package:domain/model/article_domain_model.dart';
import 'package:domain/model/banner_domain_model.dart';
import 'package:domain/usecases/banner_use_case.dart';
import 'package:domain/usecases/home_article_use_case.dart';
import 'package:flutter/material.dart';
import 'package:get/get_rx/get_rx.dart';
import 'package:get/get_state_manager/get_state_manager.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

class HomeTabController extends GetxController {
  final BannerUseCase _bannerUseCase;
  final HomeArticleUseCase _homeArticleUseCase;
  final RefreshController refreshController = RefreshController();

  final banners = <BannerDomainModel>[].obs;
  final articles = <ArticleDomainModel>[].obs;

  var _currentPageIndex = 0;

  HomeTabController({
    required BannerUseCase bannerUseCase,
    required HomeArticleUseCase homeArticleUseCase,
  }) : _bannerUseCase = bannerUseCase,
       _homeArticleUseCase = homeArticleUseCase;

  Future<void> loadData() async {
    banners.value = await _bannerUseCase.fetchBanners();
    final loadArticles = await _homeArticleUseCase.fetchArticles(
      page: _currentPageIndex,
    );
    articles.addAll(loadArticles);
  }

  Future<void> loadMore() async {
    ++_currentPageIndex;
    final loadMoreArticles = await _homeArticleUseCase.fetchArticles(
      page: _currentPageIndex,
    );
    articles.addAll(loadMoreArticles);

    if (loadMoreArticles.length < 20) {
      refreshController.loadNoData();
    } else {
      refreshController.loadComplete();
    }
  }

  Future<void> foo() async {
    final result = await _bannerUseCase.fetchBanners();
    debugPrint(result.toString());
  }
}
